package com.rr;

import com.alibaba.excel.EasyExcel;
import com.rr.entity.Student;
import com.rr.util.MemoryUsageUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ExcelCompareDemo {
    public static void main(String[] args) {
        // 准备测试数据
        List<Student> students = generateTestData(100000);
        
        // 测试EasyExcel写入
        testEasyExcel(students);
        
        // 清理内存
        System.gc();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        // 测试Apache POI写入
        testPOI(students);
    }
    
    private static void testEasyExcel(List<Student> students) {
        System.out.println("=== EasyExcel测试开始 ===");
        long startMemory = getMemoryUsage();
        long startTime = System.currentTimeMillis();

        MemoryUsageUtils.printThreadInfo();

        // EasyExcel写入
        EasyExcel.write("easyexcel_test.xlsx", Student.class)
                .sheet(0)
                .doWrite(students);
        
        long endTime = System.currentTimeMillis();
        long endMemory = getMemoryUsage();

        MemoryUsageUtils.printThreadInfo();
        
        System.out.println("EasyExcel耗时：" + (endTime - startTime) + "ms");
        System.out.println("EasyExcel内存消耗：" + (endMemory - startMemory) / 1024 / 1024 + "MB");
        System.out.println("=== EasyExcel测试结束 ===\n");
    }
    
    private static void testPOI(List<Student> students) {
        System.out.println("=== Apache POI测试开始 ===");
        long startMemory = getMemoryUsage();
        long startTime = System.currentTimeMillis();

        MemoryUsageUtils.printThreadInfo();

        try (Workbook workbook = new XSSFWorkbook()) {
            Sheet sheet = workbook.createSheet("学生信息");
            
            // 创建标题行
            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("班级编号");
            headerRow.createCell(1).setCellValue("姓名");
            headerRow.createCell(2).setCellValue("年龄");
            headerRow.createCell(3).setCellValue("生日");
            headerRow.createCell(4).setCellValue("身高");
            
            // 写入数据
            for (int i = 0; i < students.size(); i++) {
                Student student = students.get(i);
                Row row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(student.getClassNumber());
                row.createCell(1).setCellValue(student.getName());
                row.createCell(2).setCellValue(student.getAge());
                row.createCell(3).setCellValue(student.getBirthday());
                row.createCell(4).setCellValue(student.getHeight());
            }
            
            // 写入文件
            try (FileOutputStream fos = new FileOutputStream("poi_test.xlsx")) {
                workbook.write(fos);
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        long endTime = System.currentTimeMillis();
        long endMemory = getMemoryUsage();

        MemoryUsageUtils.printThreadInfo();

        System.out.println("Apache POI耗时：" + (endTime - startTime) + "ms");
        System.out.println("Apache POI内存消耗：" + (endMemory - startMemory) / 1024 / 1024 + "MB");
        System.out.println("=== Apache POI测试结束 ===");
    }
    
    private static List<Student> generateTestData(int count) {
        List<Student> students = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            Student student = new Student();
            student.setClassNumber(i);
            student.setName("张三" + i);
            student.setAge(20);
            student.setBirthday(new Date());
            student.setHeight(175.0);
            students.add(student);
        }
        return students;
    }
    
    private static long getMemoryUsage() {
        Runtime runtime = Runtime.getRuntime();
        return runtime.totalMemory() - runtime.freeMemory();
    }
}
